<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Workerman\Protocols;

use Workerman\Connection\UdpConnection;

/**
 * Frame Protocol.
 */
class Xieyi_udp
{

    public static function input($buffer)
    {

        if (strlen($buffer) < 4) {
            return 0;
        }

        $unpack_data = unpack('Ntotal_length', $buffer);

        return $unpack_data['total_length'];
    }


    public static function decode($buffer)
    {
         //return $buffer;
         //$len=substr($buffer, 0,4);
         //$unpack_data1 =unpack('Ntotal_length', $len);

        $entype=substr($buffer,0,2);

        $unpack_data = unpack('nentype', $entype);

        $entype=$unpack_data['entype'];

        $len=substr($buffer,2,2);

        $unpack_data1 = unpack('ntotal_length', $len);



         $buffer1=substr($buffer, 0,$unpack_data1['total_length']+4);
         $buffer1=substr($buffer1,4);

         //解第二组数据
        $len=substr($buffer, $unpack_data1['total_length']+4,4);

        $unpack_data2 =unpack('Ntotal_length', $len);

        $buffer2=substr($buffer, $unpack_data1['total_length']+4,$unpack_data2['total_length']+4);
        $buffer2=substr($buffer2,4);

        $data=array();
        $data[]=$buffer1;
        $data[]=$buffer2;
        $data[]=$entype;

        $buffer1=null;
        $buffer2=null;
        $entype=null;

        $unpack_data1=null;
        $unpack_data2=null;

        $len=null;



        return $data;


    }


    public static function encode($buffer)
    {
        //$total_length = strlen($buffer);
        return $buffer;
        //return pack('N', $total_length). $buffer;
    }
}
